package com.moetube.user.controller;

import com.moetube.entity.Result;
import com.moetube.entity.StatusCode;
import com.moetube.user.pojo.Collect;
import com.moetube.user.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        try {
            List<Collect> collectList = collectService.findAll();
            return new Result(true, StatusCode.OK,"查询成功",collectList);
        } catch (Exception e) {
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id){
        try {
            Collect collect = collectService.findById(id);
            return new Result(true,StatusCode.OK,"查询成功",collect);
        } catch (Exception e) {
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    /***
     * 新增数据
     * @param collect
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Collect collect){
        try {
            collectService.add(collect);
            return new Result(true,StatusCode.OK,"添加成功");
        } catch (Exception e) {
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        try {
            collectService.delete(id);
            return new Result(true,StatusCode.OK,"删除成功");
        } catch (Exception e) {
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
    }
    @GetMapping("/user/{userId}")
    public Result<List<Collect>> findCollectsListByUserId(@PathVariable("userId") String userId){
        List<Collect> list = collectService.findCollectsListByUserId(userId);
        return new Result<>(true,StatusCode.OK,"查询成功",list);

    }
}
